import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'

Vue.use(Vuex)
Vue.config.productionTip = false

const store = new Vuex.Store({
  state: {
    count: 0,
  },
  // <button @click="$store.commit()">count++</button> 
  // commit 操作
  mutations: {
    increment(state) {
      state.count ++ 
    }
  },
  // 异步操作
  // <button @click="$store.dispatch('increment')">count++</button>
  actions: {
    increment({state}) {
      setTimeout(()=>{
        state.count ++ 
      },2000)      
    }
  },
  // 缓存作用
  getters:{
    doubleCount(state) {
      return state.count * 2
    }
  }

})

new Vue({
  store,
  render: h => h(App),
}).$mount('#app')
